package com.health.memberbackstage.web;

import com.health.memberbackstage.pojo.R.Result;
import com.health.memberbackstage.pojo.Setmeal;
import com.health.memberbackstage.pojo.SetmealDetails;
import com.health.memberbackstage.service.SetmealDetailsService;
import com.health.memberbackstage.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController{

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDetailsService setmealDetailsService;

    @RequestMapping("/img")
    public void download(String name, HttpServletResponse response){
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File("D:\\upload/" + name));
            System.out.println("进入图片回显方法");
            System.out.println(fileInputStream);
            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }

            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping
    private Result findAll(){
        List<Setmeal> setmeals = setmealService.list();
        if (setmeals != null){
            return Result.success(setmeals);
        }
        return Result.fail("查询失败");
    }

    @GetMapping("/sex/{sex}")
    private Result sex(@PathVariable("sex") String sex){
        int sexInt = 0;
        if (sex.equals("不限")){
            return this.findAll();
        }else if (sex.equals("男")){
            sexInt = 1;
        }else if (sex.equals("女")){
            sexInt = 2;
        }
        List<Setmeal> setmeals = setmealService.findBySex(sexInt);
        if (setmeals != null){
            return Result.success(setmeals);
        }
        return Result.fail("查询错误");
    }

    @GetMapping("/{sid}")
    private Result findBySID(@PathVariable("sid") Long sid){

        Setmeal setmeal = setmealService.getById(sid);
        int count = setmealDetailsService.findSetmealInItemCount(sid);

        SetmealDetails setmealDetails = new SetmealDetails();
        setmealDetails.setSetmeal(setmeal);
        setmealDetails.setSetmealCount(count);

        if (setmealDetails != null){
            return Result.success(setmealDetails);
        }
        return Result.fail("查询失败");
    }
}
